package com.fofia.iotweb.web.dbdomain.services.admin;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.fofia.iotweb.common.IdHelper;
import com.fofia.iotweb.framework.dto.DictDto;
import com.fofia.iotweb.web.dbdomain.mappers.admin.SysDictMapper;
import com.fofia.iotweb.web.dbdomain.models.admin.SysDept;
import com.fofia.iotweb.web.dbdomain.models.admin.SysDictionary;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.LoginUserDto;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class SysDictService {

    @Inject("db")
    private SysDictMapper dictMapper;


    /**
     * 分页数据
     * @param dto
     * @param dicName
     * @return
     */
    public DataTableDto getPageList(DataTableDto dto,String dicName,String dictCode,long pid){

        QueryWrapper query=QueryWrapper.create();
        if(!StrUtil.isEmpty(dictCode)){
            query.like(SysDictionary::getDicCode,dictCode);
        }
        if(!StrUtil.isEmpty(dicName)){
            query.like(SysDictionary::getDicName,dicName);
        }
        query.where(SysDictionary::getParentId).eq(pid);
        query.orderBy(SysDictionary::getSortNum);

        Page<SysDictionary> pageData= dictMapper.paginate(dto.getPageNumber(),dto.getPageSize(),query);
        var list=pageData.getRecords();

        dto.setRows(list);
        dto.setTotal(pageData.getTotalRow());
        return dto;
    }


    public SysDictionary getModel(long id){

        SysDictionary dept= dictMapper.selectOneById(id);
        return dept;
    }


    public List<DictDto> getDicSelList()
    {
        QueryWrapper query=QueryWrapper.create()
          .where(SysDictionary::getParentId).eq(0)
           .orderBy(SysDictionary::getSortNum).asc();
        List<SysDictionary> list=dictMapper.selectListByQuery(query);


        List<DictDto>  data=new ArrayList<>();
        list.forEach(s->{
            DictDto dto=new DictDto();
            dto.setId(s.getId().toString());
            dto.setName(s.getDicName());
            data.add(dto);
        });

        return data;
    }

    public void saveData(SysDictionary dto, LoginUserDto userDto){


        if(dto.getParentId()>0){
            SysDictionary parentDict=dictMapper.selectOneById(dto.getParentId());
            if(parentDict!=null){
                dto.setDicCode(parentDict.getDicCode());
            }
        }
        if(dto.getId()==0){
         dto.setId(IdHelper.nextId());
         dto.setCreatePerson(userDto.getLoginName());
         dto.setCreateTime(DateUtil.date());
         dictMapper.insert(dto);
         return;
        }

       dictMapper.update(dto);
    }
    /**
     * 删除父项和子项目
     * @param ids
     */

    public void removeAll(String ids){
        List<Long> idArray = Arrays.asList(Convert.toLongArray(ids)) ;

        dictMapper.deleteBatchByIds(idArray);
        QueryWrapper query=QueryWrapper.create()
                .where(SysDictionary::getParentId).in(idArray);
        dictMapper.deleteByQuery(query);
    }

    /**
     * 删除父项和子项目
     * @param ids
     */

    public void removeSubAll(String ids){
        List<Long> idArray = Arrays.asList(Convert.toLongArray(ids)) ;
        dictMapper.deleteBatchByIds(idArray);

    }
}
